/**
 * @Title: model.js
 * @Description: model
 * @author: Fanchao
 * @date: 2020/11/11 2:05 下午
 * @version V1.0
 */

import { pathToRegexp } from "path-to-regexp";
import { message } from "antd";
import { getChildCascader, getChildTreeData } from "utils/index";
import {
    getDepartmentTreeList,
    createCourse,
    deleteCourse,
    getPagedCourseList,
    changeCourse,
    getTeacherUserModelList,
    getTrainingTypeList
} from "api/index";

export default {
    namespace: "course",
    state: {
        dataSource: {},
        keyword: "",
        reqPageInfo: {
            pageNumber: 1,
            pageSize: 10
        },
        modalTitle: "新增课程",
        modalVisible: false,
        itemData: {},
        treeData: [],
        teacherOptions: [],
        trainingTypeList: []
    },
    reducers: {
        setDataSource(state, { payload: { dataSource, keyword, reqPageInfo } }) {
            return {
                ...state,
                dataSource,
                keyword,
                reqPageInfo
            };
        },
        setModalVisible(state, { payload: modalVisible }) {
            return { ...state, modalVisible };
        },
        setModalTitle(state, { payload: modalTitle }) {
            return { ...state, modalTitle };
        },
        setItemData(state, { payload: itemData }) {
            return { ...state, itemData };
        },
        setUnitsTreeData(state, { payload: treeData }) {
            return { ...state, treeData };
        },
        setTeacherOptions(state, { payload: teacherOptions }) {
            return { ...state, teacherOptions };
        },
        setTrainingTypeList(state, { payload: trainingTypeList }) {
            return { ...state, trainingTypeList };
        }
    },
    effects: {
        /**
         * @desc 课程列表
         * @param payload
         * @param call
         * @param put
         * @param select
         * @returns {Generator<*, void, *>}
         */* getDataSource({ payload }, { call, put, select }) {
            const result = yield call(getPagedCourseList, payload);
            if (result.status === "success") {
                const dataSource = result.result;
                yield put({
                    type: "setDataSource",
                    payload: {
                        dataSource,
                        keyword: payload.keyword,
                        reqPageInfo: payload.reqPageInfo
                    }
                });
            } else {
                message.error(result.message);
            }
        },
        /**
         * @desc 获取单位
         * @param payload
         * @param call
         * @param put
         * @param select
         * @returns {Generator<*, void, *>}
         */* getUnitsTreeData({ payload }, { call, put, select }) {
            const result = yield call(getDepartmentTreeList, payload);
            if (result.status === "success") {
                const data = result.result;
                const treeData = data.map(c => {
                    if (!c.hasLowerDepartment) {
                        return {
                            title: c.departmentName,
                            key: c.departmentId,
                            comment: c.comment,
                            hasLowerDepartment: c.hasLowerDepartment,
                            level: c.level
                        };
                    } else {
                        return {
                            title: c.departmentName,
                            key: c.departmentId,
                            comment: c.comment,
                            hasLowerDepartment: c.hasLowerDepartment,
                            level: c.level,
                            children: getChildTreeData(c.lowerDepartmentModel)
                        };
                    }
                });
                yield put({
                    type: "setUnitsTreeData",
                    payload: treeData
                });
            } else {
                message.error(result.message);
            }
        },
        /**
         * @desc 新增课程
         * @param payload
         * @param callback
         * @param call
         * @param put
         * @param select
         * @returns {Generator<*, void, *>}
         */* addCourse({ payload, callback }, { call, put, select }) {
            const result = yield call(createCourse, payload);
            if (result.status === "success") {
                message.success(`添加成功`);
                if (callback && typeof callback === "function") {
                    callback(result);
                }
            } else {
                message.error(result.message);
            }
        },
        /**
         * @desc 修改课程
         * @param payload
         * @param callback
         * @param call
         * @param put
         * @param select
         * @returns {Generator<*, void, *>}
         */* updateCourse({ payload, callback }, { call, put, select }) {
            const result = yield call(changeCourse, payload);
            if (result.status === "success") {
                message.success(`修改${payload.courseName}成功`);
                if (callback && typeof callback === "function") {
                    callback(result);
                }
            } else {
                message.error(result.message);
            }
        },
        /**
         * @desc 删除课程
         * @param payload
         * @param callback
         * @param call
         * @param put
         * @param select
         * @returns {Generator<*, void, *>}
         */* removeCourse({ payload, callback }, { call, put, select }) {
            const result = yield call(deleteCourse, payload);
            if (result.status === "success") {
                message.success(`删除成功`);
                if (callback && typeof callback === "function") {
                    callback(result);
                }
            } else {
                message.error(result.message);
            }
        },
        /**
         * @desc 教员列表数据
         * @param payload
         * @param call
         * @param put
         * @param select
         * @returns {Generator<*, void, *>}
         */* getTeacherOptionList({ payload }, { call, put, select }) {
            const result = yield call(getTeacherUserModelList, payload);
            if (result.status === "success") {
                const teacherOptions = result.result;
                yield put({
                    type: "setTeacherOptions",
                    payload: teacherOptions
                });
            } else {
                message.error(result.message);
            }
        },
        * getTrainingTypeListFetch({ payload }, { call, put, select }) {
            const result = yield call(getTrainingTypeList, payload);
            if (result.status === "success") {
                const trainingTypeList = result.result;
                yield put({
                    type: "setTrainingTypeList",
                    payload: trainingTypeList
                });
            } else {
                message.error(result.message);
            }
        }
    },
    subscriptions: {
        setup({ dispatch, history }) {
            history.listen(({ pathname }) => {
                if (pathToRegexp("/course").exec(pathname)) {
                }
            });
        }
    }
};
